package Controller.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;

import Model.Newtons;
import Model.RegulaFalsi;
import View.MainView;

public class RegulaFalsiIncreaseListener implements ActionListener {

	private MainView view;
	private RegulaFalsi model;
	private int ctr = 0;
	private int max = 0;
	private int truemax = 0;
	
	public RegulaFalsiIncreaseListener(MainView view, RegulaFalsi model) {
		this.view = view;
		this.model = model;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		model.clearList();
		model.setValues(view.getX0(), view.getX1(), view.getRIteration(), view.getRFunction(), view.getRThreshold());
		model.computeRegulaFalsi();
		view.resetRegulaFalsi();
		max = model.getx0List().size() - 1;
		truemax = model.getTrueXList().size() - 1;
		
		view.setComputeRegulaFalsiButtonEnabled(false);
		
		final Timer timer = new Timer(); 
		timer.schedule( new TimerTask() {
			public void run() {
				if(ctr <= max)
					view.addRegulaFalsiRow(model.getx0List().get(ctr), model.getx1List().get(ctr), model.getx2List().get(ctr),
						model.gety0List().get(ctr), model.gety1List().get(ctr), model.gety2List().get(ctr), model.getThresholdList().get(ctr));
				if(ctr <= truemax)
					view.addRegulaFalsiList(model.getTrueXList().get(ctr), model.getTrueYList().get(ctr));
				view.updateRegulaFalsiChart();
				ctr++;
				if(ctr >  max && ctr > truemax) {
					if(ctr <= max)
						view.addRegulaFalsiRow(model.getx0List().get(ctr), model.getx1List().get(ctr), model.getx2List().get(ctr),
							model.gety0List().get(ctr), model.gety1List().get(ctr), model.gety2List().get(ctr), model.getThresholdList().get(ctr));
					if(ctr <= truemax)
						view.addRegulaFalsiList(model.getTrueXList().get(ctr), model.getTrueYList().get(ctr));
					view.updateRegulaFalsiChart();
					ctr = 0;
					timer.cancel();
					view.setComputeRegulaFalsiButtonEnabled(true);
				}		
			 } 
		}, 1000, 2000);		
	}
}
